package com.efast.dmom.mes.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.efast.cafe.util.StringUtil;
import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.WorkCenter;
import com.efast.dmom.mes.datahandlers.entity.WorkCenterHandler;
import com.efast.dmom.mes.service.WorkCenterService;
import com.efast.dmom.mes.util.StringVerifyUtil;
@Service
public class WorkCenterServiceImpl implements WorkCenterService {
	@Autowired
	private WorkCenterHandler workCenterHandler;

	@Override
	public ResultBean save(WorkCenter workCenter) {
		ResultBean bean = new ResultBean(true,"新增成功！");
		if (!StringUtil.isNotBlank(workCenter.getInnerData().getName())
				&& !StringUtil.isNotBlank(workCenter.getInnerData().getWorkCenterCode())) {
			bean.setSuccess(false);
			bean.setMessage("产线代码不能为空！");
			return bean;
		}
		
		if (!StringVerifyUtil.isstring(workCenter.getInnerData().getWorkCenterCode())) {
			bean.setSuccess(false);
			bean.setMessage("工作中心编码不是由英文、字母、下划线组成!!");
		    return bean;
		   }
		WorkCenter workCenterByName = workCenterHandler.getByName(workCenter.getInnerData().getName());
		WorkCenter workCenterByCode = workCenterHandler.getByCode(workCenter.getInnerData().getWorkCenterCode());
		if (workCenterByName.getInnerData() != null) {
			bean.setSuccess(false);
			bean.setMessage("产线名称不唯一！");
			return bean;
		}
		if (workCenterByCode.getInnerData() != null) {
			bean.setSuccess(false);
			bean.setMessage("车间编码不唯一!");
			return bean;
		}
		ResultBean resultBean = workCenterHandler.save(workCenter);
			if(!resultBean.isSuccess()) {
				bean.setSuccess(false);
				bean.setMessage(resultBean.getMessage());
				return resultBean;
			}
		bean.setObject(resultBean.getObject());
		return bean;
	}

	@Override
	public ResultBean update(WorkCenter workCenter) {
		ResultBean resultBean = new ResultBean(true,"编辑成功！");
		WorkCenter workCenterById = workCenterHandler.getById(workCenter.getInnerData().getId());
		if (workCenterById.getInnerData() == null) {
			resultBean.setSuccess(false);
			resultBean.setMessage("修改的工作中心不存在!");
			return resultBean;
		}
		if (!StringVerifyUtil.isstring(workCenter.getInnerData().getWorkCenterCode())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工作中心编码不是由英文、字母、下划线组成!!");
		    return resultBean;
		   }
		if (!workCenter.getInnerData().getWorkCenterCode().equals(workCenterById.getInnerData().getWorkCenterCode())
				&& !workCenter.getInnerData().getName().equals(workCenterById.getInnerData().getName())) {
			resultBean.setSuccess(false);
			resultBean.setMessage("工作中心编码、工作中心名称不可修改!");
			return resultBean;
		}
		workCenterHandler.update(workCenter);
		resultBean.setObject(workCenter);
		return resultBean;
	}

	@Override
	public ResultBean delete(WorkCenter workCenter) {
		ResultBean resultBean = new ResultBean(true,"删除成功！");
		int queryStationById = workCenterHandler.queryStationById(workCenter.getInnerData().getId());
		if (queryStationById >0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("此工作中心下有工位，不可删除！");
			return resultBean;
		}
		int queryEquipmentById = workCenterHandler.queryEquipmentById(workCenter.getInnerData().getId());
		if (queryEquipmentById >0) {
			resultBean.setSuccess(false);
			resultBean.setMessage("此工作中心下有设备，不可删除！");
			return resultBean;
		}
		workCenterHandler.delete(workCenter);
		return resultBean;
	}

	@Override
	public WorkCenter getById(long id) {
		WorkCenter byId = workCenterHandler.getById(id);
		return byId;
	}

	@Override
	public WorkCenter getByName(String name) {
		WorkCenter byName = workCenterHandler.getByName(name);
		return byName;
	}

	@Override
	public WorkCenter getByCode(String workCenterCode) {
		WorkCenter byCode = workCenterHandler.getByCode(workCenterCode);
		return byCode;
	}

	@Override
	public List<WorkCenter> getAllWorkCenter() {
		return  workCenterHandler.getAllWorkCenter();
	}

	@Override
	public ResultBean getByLineId(long lineId) {
		ResultBean resultBean = new ResultBean(true,"");
		List<WorkCenter> listWorkCenter = workCenterHandler.getByLineId(lineId);
		resultBean.setObject(listWorkCenter);
		return resultBean;
	}

}
